/*     */ package com.cr.framework.shiro.realm;
/*     */ 
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.exception.user.CaptchaException;
/*     */ import com.cr.common.exception.user.RoleBlockedException;
/*     */ import com.cr.common.exception.user.UserBlockedException;
/*     */ import com.cr.common.exception.user.UserNotExistsException;
/*     */ import com.cr.common.exception.user.UserPasswordNotMatchException;
/*     */ import com.cr.common.exception.user.UserPasswordRetryLimitExceedException;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import com.cr.framework.shiro.service.SysLoginService;
/*     */ import com.cr.system.service.ISysMenuService;
/*     */ import com.cr.system.service.ISysRoleService;
/*     */ import java.util.HashSet;
/*     */ import java.util.Set;
/*     */ import org.apache.shiro.authc.AuthenticationException;
/*     */ import org.apache.shiro.authc.AuthenticationInfo;
/*     */ import org.apache.shiro.authc.AuthenticationToken;
/*     */ import org.apache.shiro.authc.ExcessiveAttemptsException;
/*     */ import org.apache.shiro.authc.IncorrectCredentialsException;
/*     */ import org.apache.shiro.authc.LockedAccountException;
/*     */ import org.apache.shiro.authc.SimpleAuthenticationInfo;
/*     */ import org.apache.shiro.authc.UnknownAccountException;
/*     */ import org.apache.shiro.authc.UsernamePasswordToken;
/*     */ import org.apache.shiro.authz.AuthorizationInfo;
/*     */ import org.apache.shiro.authz.SimpleAuthorizationInfo;
/*     */ import org.apache.shiro.cache.Cache;
/*     */ import org.apache.shiro.realm.AuthorizingRealm;
/*     */ import org.apache.shiro.subject.PrincipalCollection;
/*     */ import org.apache.shiro.subject.SimplePrincipalCollection;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ 
/*     */ public class UserRealm extends AuthorizingRealm {
/*  42 */   private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
/*     */   
/*     */   @Autowired
/*     */   private ISysMenuService menuService;
/*     */   
/*     */   @Autowired
/*     */   private ISysRoleService roleService;
/*     */   
/*     */   @Autowired
/*     */   private SysLoginService loginService;
/*     */   
/*     */   protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
/*  59 */     SysUser user = ShiroUtils.getSysUser();
/*  61 */     Set<String> roles = new HashSet<>();
/*  63 */     Set<String> menus = new HashSet<>();
/*  64 */     SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
/*  66 */     if (user.isAdmin()) {
/*  68 */       info.addRole("admin");
/*  69 */       info.addStringPermission("*:*:*");
/*     */     } else {
/*  73 */       roles = this.roleService.selectRoleKeys(user.getUserId());
/*  74 */       menus = this.menuService.selectPermsByUserId(user.getUserId());
/*  76 */       info.setRoles(roles);
/*  78 */       info.setStringPermissions(menus);
/*     */     } 
/*  80 */     return (AuthorizationInfo)info;
/*     */   }
/*     */   
/*     */   protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
/*  89 */     UsernamePasswordToken upToken = (UsernamePasswordToken)token;
/*  90 */     String username = upToken.getUsername();
/*  91 */     String password = "";
/*  92 */     if (upToken.getPassword() != null)
/*  94 */       password = new String(upToken.getPassword()); 
/*  97 */     SysUser user = null;
/*     */     try {
/* 100 */       user = this.loginService.login(username, password);
/* 102 */     } catch (CaptchaException e) {
/* 104 */       throw new AuthenticationException(e.getMessage(), e);
/* 106 */     } catch (UserNotExistsException e) {
/* 108 */       throw new UnknownAccountException(e.getMessage(), e);
/* 110 */     } catch (UserPasswordNotMatchException e) {
/* 112 */       throw new IncorrectCredentialsException(e.getMessage(), e);
/* 114 */     } catch (UserPasswordRetryLimitExceedException e) {
/* 116 */       throw new ExcessiveAttemptsException(e.getMessage(), e);
/* 118 */     } catch (UserBlockedException e) {
/* 120 */       throw new LockedAccountException(e.getMessage(), e);
/* 122 */     } catch (RoleBlockedException e) {
/* 124 */       throw new LockedAccountException(e.getMessage(), e);
/* 126 */     } catch (Exception e) {
/* 128 */       log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
/* 129 */       throw new AuthenticationException(e.getMessage(), e);
/*     */     } 
/* 131 */     SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
/* 132 */     return (AuthenticationInfo)info;
/*     */   }
/*     */   
/*     */   public void clearCachedAuthorizationInfo(Object principal) {
/* 140 */     SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
/* 141 */     clearCachedAuthorizationInfo((PrincipalCollection)principals);
/*     */   }
/*     */   
/*     */   public void clearAllCachedAuthorizationInfo() {
/* 149 */     Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
/* 150 */     if (cache != null)
/* 152 */       for (Object key : cache.keys())
/* 154 */         cache.remove(key);  
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\shiro\realm\UserRealm.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */